package com.app.scale.bean;


import android.content.Context;
import android.text.TextUtils;

import com.app.scale.basis.Constants;
import com.app.scale.jpush.TagAliasOperatorHelper;

import java.util.ArrayList;
import java.util.List;

import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;


public class LoginBean extends BasisBean {
    private static final long serialVersionUID = 1L;

    public static final int ERRO_CODE_DEVICE = -3001;


    public static final int LOGIN_TYPE_PERSON = 0;
    public static final int LOGIN_TYPE_FA = 1;

    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "login.data";
    private static LoginBean sLoginBean;


    private String token;
    private String scope;
    String uid;
    String uno;     //工号
    String uname;      //姓名
    String ufsign;
    String aid;
    String aname;
    String bname;          //简称
    String acode;
    private List<MenuBean> right;




    public LoginBean() {
        super();
        // sLoginBean = this;
    }

    public static boolean isLogin() {
		return getInstance() != null && !TextUtils.isEmpty(getInstance().getToken());
    }

    public static LoginBean getInstance() {
        if (sLoginBean == null)
            sLoginBean = getFromCache();
        return sLoginBean;
    }

    public static LoginBean getFromCache() {
        LoginBean data;
        data = (LoginBean) FileUtils.readObject(FILE_CACHE);
        if (data != null) {
            sLoginBean = data;
        }
        LogUtil.i("login---->getFromCache---" + (data == null));
        return data;
    }

    public void save() {
        sLoginBean = this;
        FileUtils.cacheObject(FILE_CACHE, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
        LogUtil.i("login---->save---" + FILE_CACHE);
    }

    private static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

	public static void logout() {
		delete();
		sLoginBean = null;
//        TagAliasOperatorHelper.getInstance().cleanAlias();
	}

    public static void logout(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
        mSp.putPassword("");
        mSp.putUser("");
        TagAliasOperatorHelper.getInstance().cleanAlias();
    }

    public static void logPast(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }


//    public   String getJpushAlias(){
//        return "ukluser_"+getUserId();
//    }

    public String getUfsName(){
        String typeName = "";
        switch (getUfs()){
            case "2":
                typeName = "计重员";
                break;
            case "3":
                typeName = "计价员";
                break;
            case "4":
                typeName = "管理员";
                break;
        }
        return typeName;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }


    public static String getUserToken() {
        if (!isLogin()) return "";
        return getInstance().getToken();
    }

    public static String getUId() {
        if (!isLogin()) return "";
        return getInstance().getUid();
    }

    public static String getUNo() {
        if (!isLogin()) return "";
        return getInstance().getUno();
    }

    public static String getUName() {
        if (!isLogin()) return "";
        return getInstance().getUname();
    }
    public static String getUFsign() {
        if (!isLogin()) return "";
        return getInstance().getUfsign();
    }
    public static String getAId() {
        if (!isLogin()) return "";
        return getInstance().getAid();
    }
    public static String getAName() {
        if (!isLogin()) return "";
        return getInstance().getAname();
    }

    public static String getBName() {
        if (!isLogin()) return "";
        return getInstance().getBname();
    }

    public static String getACode(){
        if (!isLogin()) return "null";
        return getInstance().getAcode();
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }


    public List<MenuBean> getRight() {
        return right;
    }

    public void setRight(List<MenuBean> right) {
        this.right = right;
    }


    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUno() {
        return uno;
    }

    public void setUno(String uno) {
        this.uno = uno;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUfsign() {
        return ufsign;
    }

    public void setUfsign(String ufsign) {
        this.ufsign = ufsign;
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getAcode() {
        return acode;
    }

    public void setAcode(String acode) {
        this.acode = acode;
    }

    public List<MenuBean> getParentMenuList(){
        List<MenuBean> parentMenuList = new ArrayList<>();
        if(getInstance().getRight() == null){
            return null;
        }
        for (MenuBean menuBean : getInstance().getRight()){
             if (menuBean.getPid() == 0){
                 parentMenuList.add(menuBean);
             }
        }
        return parentMenuList;
    }

    public MenuBean getMenuBeanByMna(String mna){
        if(getInstance().getRight()== null){
            return null;
        }else {
            for(MenuBean menuBean : getInstance().getRight()){
                if(menuBean.getMna().contains(mna)){
                    return menuBean;
                }
            }
            return null;
        }
    }

    public boolean canJijia(){
        if(TextUtils.isEmpty(ufs) || "null".equals(ufs)){
            return false;
        }
        switch (ufs){    //4代表管理员，可以直接输入单价    3是计价员
            case "3":
            case "4":
                    return true;
            default:
                return false;
        }
    }

    public boolean isAdmin(){
        if(TextUtils.isEmpty(ufs) || "null".equals(ufs)){
            return false;
        }
        switch (ufs){    //4代表管理员
            case "4":
                return true;
            default:
                return false;
        }
    }
}
